package yd.launcher;

import yd.launcher.service.DataService;
import yd.launcher.service.Panel;

import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.plaf.metal.MetalIconFactory;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class MainFrame extends JFrame{
    //menus
    private JMenu menuConfig = new JMenu("Config");
    private JMenuItem miAddPanel = new JMenuItem("Add Panel");
    private JMenu miEditPanel = new JMenu("Edit Panel");
    private JMenu miDelPanel = new JMenu("Delete Panel");
    private JCheckBoxMenuItem alwaysOnTop = new JCheckBoxMenuItem("AlwaysOnTop");
    //contents
    private JPanel mainPanel = new JPanel();

    private DataService dataService = R.dataService;
    private List<Panel> panels = R.dataService.getPanels();

    public MainFrame(){
        super("My Launcher");
        initMenuBar();
        //initToolBar();
        initMainPanel();
        //this.setIconImage(new ImageIcon(getClass().getResource("/images/icon.ico")).getImage());
        this.setIconImage(R.iconImage);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(700,700);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                Rectangle bounds = getBounds();
                dataService.setBounds(bounds.width, bounds.height, bounds.x, bounds.y);
                dataService.saveToFile(R.CONFIG_FILE_PATH);
            }
        });
        if (dataService.getBounds() != null) {
            int[] bounds = dataService.getBounds();
            setSize(bounds[0], bounds[1]);
            setLocation(bounds[2], bounds[3]);
        }
    }

    private void initMenuBar() {
        miAddPanel.addActionListener(e -> {
            String s = JOptionPane.showInputDialog(this,"Name:", "Add New Panel",JOptionPane.PLAIN_MESSAGE);
            if (s != null) {
                Panel newPanel = new Panel(s, new ArrayList<>());
                panels.add(newPanel);
                mainPanel.add(new ButtonsPanel(newPanel));
                mainPanel.revalidate();
            }
        });
        miEditPanel.addMenuListener(new MenuListener() {
            @Override
            public void menuSelected(MenuEvent e) {
                for (Panel panel : panels) {
                    JMenuItem menuItem = new JMenuItem(panel.getName());
                    menuItem.addActionListener(e1 -> {
                        String s = (String)JOptionPane.showInputDialog(MainFrame.this,"Name:","Edit Panel",JOptionPane.PLAIN_MESSAGE,null,null,panel.getName());
                        if (s !=null) {
                            panel.setName(s);
                            ((ButtonsPanel)mainPanel.getComponent(panels.indexOf(panel))).refreshPanel();
                            mainPanel.repaint();
                        }
                    });
                    miEditPanel.add(menuItem);
                }
            }

            @Override
            public void menuDeselected(MenuEvent e) {
                miEditPanel.removeAll();
            }

            @Override
            public void menuCanceled(MenuEvent e) {

            }
        });
        miDelPanel.addMenuListener(new MenuListener() {
            @Override
            public void menuSelected(MenuEvent e) {
                for (Panel panel : panels) {
                    JMenuItem menuItem = new JMenuItem(panel.getName());
                    menuItem.addActionListener(e1 -> {
                        ButtonsPanel buttonsPanel = (ButtonsPanel)mainPanel.getComponent(panels.indexOf(panel));
                        if (buttonsPanel.getComponentCount() > 0) {
                            JOptionPane.showMessageDialog(MainFrame.this,"Contains not empty!");
                            return;
                        }
                        mainPanel.remove(buttonsPanel);
                        mainPanel.revalidate();
                        panels.remove(panel);
                    });
                    miDelPanel.add(menuItem);
                }
            }

            @Override
            public void menuDeselected(MenuEvent e) {
                miDelPanel.removeAll();
            }

            @Override
            public void menuCanceled(MenuEvent e) {

            }
        });

        alwaysOnTop.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    setAlwaysOnTop(true);
                }else{
                    setAlwaysOnTop(false);
                }
            }
        });
        menuConfig.add(miAddPanel);
        menuConfig.add(miEditPanel);
        menuConfig.add(miDelPanel);
        menuConfig.addSeparator();
        menuConfig.add(alwaysOnTop);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menuConfig);
        this.setJMenuBar(menuBar);
    }

    private void initToolBar(){
        JToolBar toolbar = new JToolBar("工具栏");
        toolbar.setFloatable(true);
        Icon icon = MetalIconFactory.getFileChooserDetailViewIcon();
        JToggleButton button = new JToggleButton(icon);
        toolbar.add(button);
        icon = MetalIconFactory.getFileChooserHomeFolderIcon();
        button = new JToggleButton(icon);
        toolbar.add(button);
        icon = MetalIconFactory.getFileChooserListViewIcon();
        button = new JToggleButton(icon);
        toolbar.add(button);
        this.add(toolbar, BorderLayout.NORTH);
    }

    private void initMainPanel(){
        mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
        for (Panel panel : panels) {
            mainPanel.add(new ButtonsPanel(panel));
        }
        add(mainPanel);
    }
}

